# Build recipe for libelf (elfutils).
#
# Copyright (c) 2019 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

program=elfutils
pkgname=libelf
version=0.178
release=1

# Set 'outdir' for a nice and well-organized output directory
outdir="${outdir}/${arch}/devel"

tarname=${program}-${version}.tar.bz2

# Remote source(s)
fetch=http://sourceware.org/elfutils/ftp/${version}/$tarname

description="
ELF object file access library.

libelf lets you read, modify or create ELF files in an
architecture-independent way.  The library takes care of
size and endian issues.

These libraries was taken from the elfutils project.
"

homepage=http://sourceware.org/elfutils/
license="GPLv2+, GPLv3+, LGPLv3"

# Source documentation
docs="AUTHORS CONTRIBUTING COPYING* src/ChangeLog NEWS NOTES README THANKS TODO"
docsdir="${docdir}/${pkgname}-${version}"

build()
{
    set -e

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Apply patches from the Openembedded project
    patch -Np1 -i "${worktree}/patches/libelf/0006-Fix-build-on-aarch64-musl.patch"
    patch -Np1 -i "${worktree}/patches/libelf/musl-libs.patch"
    patch -Np1 -i "${worktree}/patches/libelf/musl-tests.patch"
    patch -Np1 -i "${worktree}/patches/libelf/musl-utils.patch"

    autoreconf -vif

    ./configure CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --docdir=$docsdir \
     --enable-deterministic-archives \
     --disable-debuginfod \
     --build="$(cc -dumpmachine)"

    make -j${jobs} V=1 -C lib
    make -j${jobs} V=1 -C libelf
    make -j${jobs} V=1 -C libelf DESTDIR="$destdir" install-strip

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}"
}

